<?php 
error_reporting(E_ALL);
$PageSecurity = 14;
include_once ('includes/session.inc');
set_time_limit(0);

$action = isset($_POST['action']) ? $_POST['action'] : '';

if ($action == 'getPartner') {
    $result = _single('api/partner')->GetAllPartner($_SESSION['UserID'], $_SESSION['Password']);
    
    $data = '{"success":true';
    $data .= ',"data":[';
    $haveData = false;
    
    while ($row = DB_fetch_array($result)) {
        $haveData = true;
        $data .= sprintf('{"partner_id":"%s","partner_number":"%s","partner_name":"%s"},', $row['partner_id'], $row['partner_number'], $row['partner_name']);
    }
    
    if ($haveData) {
        $data = substr($data, 0, strlen($data) - 1);
    }
    
    $data .= '],"count":"'.json_encode($total).'"}';
    echo $data;
} elseif ($_GET['action'] == 'export') {

    $DEFAULT_EXPORT_FILE_NAME = 'BangDanhSachChuaTungCoSanLuong';
    $DEFAULT_DIR_UPLOAD = 'tmp/exported/';
    $DEFAULT_EXCEL_TYPE = 'Excel5'; // default Excel 2003 - Excel5,Excel2007
    
    $titleObject = '';
	$titleDaidien = '';
// detail
	// lay du lieu
	$Partner = _single('api/partner')->GetPartnerById($_GET['partnerId'], $_SESSION['UserID'], $_SESSION['Password']);
	$result = _single('api/exchange')->GetSongsNerverExchanged($_GET['partnerId'], $_SESSION['UserID'], $_SESSION['Password']);
    $objPHPExcel = new Api_Excel($DEFAULT_EXPORT_FILE_NAME . "_{$Partner['partner_name']}", $DEFAULT_DIR_UPLOAD, $DEFAULT_EXCEL_TYPE);
    $objPHPExcel->setActiveSheet(0);
	
	$aligment_HorCent_VerCent = array (
		'horizontal'	=> PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
		'vertical' 		=> PHPExcel_Style_Alignment::VERTICAL_CENTER
	);
	
	$aligment_HorCent_VerBottom = array (
		'horizontal'	=> PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
		'vertical' 		=> PHPExcel_Style_Alignment::VERTICAL_BOTTOM
	);
	
    $border_thin = array (
		'style' => PHPExcel_Style_Border :: BORDER_THIN			
	);
    $border_thin_ALL_title = array(
		'borders'=>array(
			'top'	=> $border_thin, 
			'right'	=> $border_thin, 
			'bottom'=> $border_thin, 
			'left'	=> $border_thin
		)
	);
	
	$border_thin_ALL = array(
		'borders'=>array(
			'top'	=> $border_thin, 
			'right'	=> $border_thin, 
			'bottom'=> $border_thin, 
			'left'	=> $border_thin
		),
		'alignment' => $aligment_HorCent_VerBottom,
		'font' => array (
			'bold' 	=> false,
			'size' 	=> 10,
			'color' => array('argb' => 'FFFF0000')
		)
	);
    
	// set default style	
	$objPHPExcel->setPageSetup(85, PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4, PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
	$objPHPExcel->setDefaultStyle('Times New Roman', 12);
	//$objPHPExcel->setFreezePane("E13");
	$objPHPExcel->setPageMargins(0.5, 0.25, 0, 0.75);
	$objPHPExcel->setHorizontalCentered(true);		
		
	$objPHPExcel->setColumnWidth("A", 12)
				->setColumnWidth("B", 35);
	
	/* NOT FILE NAME
	/*
	if(!empty($title)){
		$title = substr($title, 0, strlen($title) - 1);
	}
	$objPHPExcel->setFileNameExcel($DEFAULT_EXPORT_FILE_NAME . "_[" . $title . "]"); */
	
	$objPHPExcel->mergeCells("A1", "K1")
				->setText("A1","BẢNG DANH SÁCH BÁI HÁT CHƯA TỪNG CÓ SẢN LƯỢNG {$Partner['partner_name']}", false, false, false, 14)
				->setCellHorizontalAllignment("A1", 'center')
				->setRowHeight(1, 28)
				
				->setText("A2","Mã số", false, false, false, 13)
				->setCellHorizontalAllignment("A2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "A2", "A2")
				
				->setText("B2","Tên bài hát", false, false, false, 13)
				->setCellHorizontalAllignment("B2", 'center')
				->setDuplicateStyleArray($border_thin_ALL_title, "B2", "B2")
				;
		
	// export detail
	$Position = 3;
	
	//while ($row = DB_fetch_array($list_song)) {
	while ($row = DB_fetch_array($result)) {
		$objPHPExcel->setText("A".($Position), $row['song_number'])
			    ->setCellHorizontalAllignment("A".($Position), 'left')
			    ->setDuplicateStyleArray($border_thin_ALL_title, "A".($Position), "A".$Position)	
			    ->setText("B".($Position), $row['song_name'])
			    ->setCellHorizontalAllignment("B".($Position), 'left')
			    ->setDuplicateStyleArray($border_thin_ALL_title, "B".($Position), "B".($Position));
					
		
		$Position++;
	}
		
	$objPHPExcel->outputToBrowser();
	exit;
}

function getCharacterColumn(&$leftcol, &$rightcol){
//	for($i = 0; $i < 55 ; $i++){
//		echo getCharacterColumn(&$right_col_index, &$left_col_index) . " ";
//	}
	$rs = '';
	if($rightcol > 90){
		$leftcol++;
		$rightcol = 65;
	}
	if($leftcol >= 65){
		$rs = chr($leftcol) . chr($rightcol);		
	}else{		
		$rs = chr($rightcol);		
	}
	
	$rightcol++;
	
	return $rs;
}
?>